package org.example.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.mapper.TraMemoryMapper;
import org.example.mapper.UserMapper;
import org.example.pojo.TraMemory;
import org.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller//方法返回的是一个页面 login.htm

public class PageController {
    //网页名
    @Autowired
    UserMapper um;

    @Autowired
    TraMemoryMapper tmm;//如果此处报错那就是没有添加@mapper,导致找不到。

    //注销登录
    @GetMapping("des")
    public void destory(HttpServletRequest req, HttpServletResponse rep){
        //清空session
        req.getSession().removeAttribute("user");
        //跳转到登录页
        //如果在这里直接跳转首页，没有查询首页数据，导致数据为空
        //可以改成调用进到首页的方法 idx
        RequestDispatcher dispatcher = req.getRequestDispatcher("/traix ");
        try {
            dispatcher.forward(req, rep);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //注销

    /*
    prefix:
    */
    //html://lodcalhost:8081/golg"8081为端口号"

    @GetMapping("golog")//括号内是名字
    public String goLogin(Model model, HttpServletRequest req) {
        //想把后台数据传到页面显示
        String name = "鲁班";//
        User u = new User();
        u.setUsername("韩信");
        u.setPassword("666");
        model.addAttribute("p1", u);
        model.addAttribute("myname", name);
        //model里的数据就会传到前台去

//        List<User> users = um.selectList(null);
//        model.addAttribute("us",users);

        //获取session对象"放公用资源"
        HttpSession session = req.getSession();
        session.setAttribute("User", u);
        //
        return "login.htm";//前面前缀与后缀已经添加过
    }

    //下面是两个跳转方法
    @GetMapping("/idx")
    public String goIndex() {
        return "index.htm";
    }

    @GetMapping("/tralg")
    public String traLogin() {
        return "html/login/login";
    }


    @GetMapping("/trarg")//注册
    public String traRegister() {
        return "html/login/register";
    }

    @GetMapping("/traix")
    public String traindex(Model model) {
        //在进入首页之前需要先把旅游记忆的数据提前拿出来放到首页中。
        //差选旅游记忆数据，
        QueryWrapper qw =new QueryWrapper();

        qw.orderByDesc("pubtime");
        //查询某个类别的前四条记录
        qw.last("LIMIT 4");//从数据库中取出前四个景点
        List<TraMemory> traMemories = tmm.selectList(qw);
        model.addAttribute("mlist",traMemories);


        return "indexR";
    }

    @GetMapping("/traimg")
    public String traimagin() {
        return "resources/static/images/001.img";
    }
    @GetMapping
    public void createCaptcha(HttpServletResponse response) throws IOException {
        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        // 图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        // 关闭流
        response.getOutputStream().close();
    }
    //添加页面跳转
    @GetMapping("/tratz")//
    public String tratiaoz(){
        return "/html/user";
    }

}


